/**
 * @copyright SilverBiology, LLC
 * @website http://www.silverbiology.com
*/

Ext.namespace('Gbif.Glossary')

Gbif.Glossary.TabPanel = function(config){

	this.welcomeTab = new Gbif.Glossary.WelcomePanel();
	this.welcomeTab.on('newTabAdd', this.addTermsTab, this);	
	
	this.infoTab = new Gbif.Glossary.InfoPanel();
		
	Ext.apply(this, config, {
			activeTab: 0
		,	deferredRender: false
		,	enableTabScroll: true
		,	items: [ this.welcomeTab, this.infoTab ]
	});

	Gbif.Glossary.TabPanel.superclass.constructor.call(this,config)

}

Ext.extend( Gbif.Glossary.TabPanel , Ext.TabPanel, {
	
		//add new tab to the array and tabPanel.
		addTab:function(vartitle, varurl, icon){
			
			var newtab = this.findTab( vartitle )
			if (newtab == null) {
				newtab = new Ext.Panel({
						title: vartitle
					,	autoLoad: varurl
					,	closable: true	
					,	unstyle: true	
					,	iconCls: icon		
					,	autoScroll: true
				});
				
				this.add( newtab );
			}
			this.setActiveTab( newtab );
		}	
		
	,	addTermsTab:function(type,title,value,icon,id){
			var newtab = null;
			newtab = this.findTab(title+': ' + value)
	
			if (newtab == null) {
				var mainRefLink = "hide";
				if (title == "Term") {
					mainRefLink = BASE_URL + "terms/letter/" + Ext.util.Format.substr(value,0,1).toUpperCase();
				}
				GBif.Stats.log( 'Terms/' + title + '/' + value );
				newtab = new Gbif.Glossary.TermsPanel({
						title: title + ': ' + value
					,	termID: id
					,	iconCls: icon
					,	mainRefLink: mainRefLink
				});

				newtab.on('newTabAdd', this.addTermsTab, this);									
				this.add(newtab);
				this.setActiveTab(newtab);
				this.doLayout();
				newtab.termsStore.baseParams.type = type;		
				newtab.termsStore.baseParams.value = value;		
				newtab.term = id;
				newtab.termsStore.load();
			} else {
				this.setActiveTab(newtab);
				newtab.scrollToTerm(id);
			}

		}
		
		//Find already added tab.
	,	findTab:function(vartitle){
		
			var findtab = null;
			this.items.each( function(rec){
				if (rec.title == vartitle) {
					findtab = rec;
				}
			});
			return findtab;
		}
		
		//remove already added tab.
	,	removeTab: function(obj){
			this.tabArray.remove( this.findTab( obj.title ) );	
		}
		
	,	stmInfoPanel:function(el){
			this.setActiveTab( this.infoTab );
			this.infoTab.showInfo( el );							
		}
			
});